package com.swf.seed.io.nio;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NioTest {
	public static void main(String[] args) throws IOException {
		NioTest test = new NioTest();
		test.read("D:/yeb/code/dev/seed/seed/seed-io/src/main/resources/in.txt");
		test.write("D:/yeb/code/dev/seed/seed/seed-io/src/main/resources/out.txt","abc".getBytes());
		
	}
	
	public void read(String location) throws IOException{
		FileInputStream fin = new FileInputStream(location);
		FileChannel fc = fin.getChannel();
		ByteBuffer buffer = ByteBuffer.allocate(1024);
		fc.read(buffer);
		fin.close();
	}
	public void write(String location,byte[] message) throws IOException{
		FileOutputStream fou = new FileOutputStream(location);
		FileChannel fc = fou.getChannel();
		ByteBuffer buffer = ByteBuffer.allocate(1024);
		for (int i=0; i<message.length; ++i) { 
		     buffer.put( message[i] ); 
		}  
		buffer.flip();
		fc.write(buffer);
		fou.close();
	}
}
